package avicit.bdp.dcs.task.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.dto.api.APIHttpDefinition;
import avicit.bdp.common.dto.api.BodyParam;
import avicit.bdp.common.dto.api.RequestParam;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.dcs.job.dto.ApiDefinitionDto;
import avicit.bdp.dcs.task.dao.ApiDefinitionDao;
import avicit.platform6.core.exception.BusinessException;
import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Service;

import java.lang.reflect.AnnotatedArrayType;
import java.util.ArrayList;

@Service
public class ApiDefinitionService extends BaseService<ApiDefinitionDao, ApiDefinitionDto> {

    /**
     * 获取API定义参数
     * @param jobId
     * @return
     */
    public APIHttpDefinition getAPIHttpDefinition(String jobId) {
        ApiDefinitionDto queryApiDefinitionDto = new ApiDefinitionDto();
        queryApiDefinitionDto.setJobId(jobId);
        ApiDefinitionDto apiDefinitionDto = this.selectOne(queryApiDefinitionDto);
        CheckParaUtils.checkDbObjectValidity(apiDefinitionDto, logger, String.format("API定义数据没查到,jobId:%s", jobId));

        APIHttpDefinition apiHttpDefinition = JSON.parseObject(apiDefinitionDto.getDefinitionJson(), APIHttpDefinition.class);
        if (apiHttpDefinition == null) {
            throw new BusinessException("请检查任务定义API参数");
        }

        return apiHttpDefinition;
    }

    public APIHttpDefinition getAPIHttpDefinitionInstance() {
        APIHttpDefinition apiHttpDefinition = new APIHttpDefinition();
        RequestParam requestParam = new RequestParam();
        requestParam.setQueryParam(new ArrayList<>());
        requestParam.setRestParam(new ArrayList<>());
        BodyParam bodyParam = new BodyParam();
        bodyParam.setBodyParamType("");
        bodyParam.setFormData(new ArrayList<>());
        bodyParam.setXwwwFormUrlencoded(new ArrayList<>());
        bodyParam.setJson("");
        requestParam.setBodyParam(bodyParam);
        apiHttpDefinition.setRequestParam(requestParam);
        apiHttpDefinition.setHeader(new ArrayList<>());
        return apiHttpDefinition;
    }

}
